package com.code.shiro.user.controller;


import com.alibaba.fastjson.JSON;
import com.code.shiro.config.JWTUtil;
import com.code.shiro.user.service.UserService;
import com.code.wanmi.entity.other.IpUtil;
import com.code.wanmi.entity.other.ResponseBean;
import com.code.wanmi.entity.wm_user.WmSysRole;
import com.code.wanmi.entity.wm_user.WmSysUser;
import com.code.wanmi.entity.wm_user.dto.SysUserDto;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-12-05
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class WmSysUserController {
    // 用户表
    @InitBinder("wmSysUser")
    public void initBinderWmSysUser(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("wmSysUser.");
    }
    //角色表
    @InitBinder("wmSysRole")
    public void initBinderPeople(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("wmSysRole.");
    }

    @Autowired
    private UserService userService;




    @ApiOperation(value="显示所有用户")
   // @RequiresRoles(value = {"总监","总裁"},logical = Logical.OR)
    @RequestMapping(value = "/select",method = RequestMethod.GET)
    public String selectUser(){
        return userService.selectUsers();
    }

    @ApiOperation(value="删除用户")
            @ApiImplicitParam(name="user" ,value="用户信息" ,required=true ,dataType="WmSysUser",paramType="query")
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功，固定返回打招呼内容，字符串表现形式；")
    })
    @RequestMapping(value = "/remove",method = RequestMethod.PATCH)
    public String removeUser(WmSysUser user){
        return userService.removeUser(user);
    }


    /**
     * 添加用户操作及分配角色
     * &对已有用户添加角色
     * @param wnSysUser
     * @param wmSysRole
     * @return
     */

    @ApiOperation(value="分配角色")
    //参数说明
    @ApiImplicitParams({

            @ApiImplicitParam(name="wmSysRole" ,value="权限" ,required=true ,dataType="WmSysUser",paramType="query"),
            @ApiImplicitParam(name="wnSysUser" ,value="用户" ,required=true ,dataType="WmSysUser",paramType="query")
    })
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功")
    })

    @RequestMapping(value = "/insertSysUser",method = RequestMethod.POST)
    public String insertUser(@ModelAttribute("wnSysUser") WmSysUser wnSysUser,
                             @ModelAttribute("wmSysRole") WmSysRole wmSysRole){
        SysUserDto dto =new SysUserDto(wnSysUser,wmSysRole);
        return userService.insertUser(dto);
    }

    /**
     * 修改用户(所有)
     * @param wmSysUser
     * @return
     */

    @ApiOperation(value="修改用户（所有）")
    @ApiImplicitParam(name="wnSysUser" ,value="用户" ,required=true ,dataType="WmSysUser",paramType="query")
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功，固定返回打招呼内容，字符串表现形式；")
    })


    @RequestMapping(value = "/updateUserRole",method = RequestMethod.PUT)
    public String updateUser(@RequestBody WmSysUser wmSysUser){

        return userService.updateUser(wmSysUser);
    }

    @ApiOperation(value="登录")
    //参数说明
    @ApiImplicitParams({
            @ApiImplicitParam(name="username" ,value="用户名" ,required=true ,dataType="string",paramType="query"),
            @ApiImplicitParam(name="password" ,value="用户密码" ,required=true ,dataType="string",paramType="query")})
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功，固定返回打招呼内容，字符串表现形式；")
    })
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam("password") String password,@RequestParam("username") String username, HttpServletRequest request){
        WmSysUser user = new WmSysUser();
        user.setLoginIp(IpUtil.getIpAddr(request));
        user.setLoginName(username);
        user.setPassword(password);
        String login = userService.login(user);
      return login;
    }


    @ApiOperation(value="获取用户权限信息")
    //参数说明
            @ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "Long", paramType = "query")
    //返回说明
    @ApiResponses({
            @ApiResponse(code=200 , message="执行成功，固定返回打招呼内容，字符串表现形式；")
    })
    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public String userInfo(HttpServletRequest request) {
        String request1 = request.getHeader("Authorization");
        Long userId = JWTUtil.getUserId(request1);
        System.out.println(userId);
        Set<WmSysRole> roles = userService.getRoleNameByUserId(userId);
        List<String> roleNames = new ArrayList<>();
        for (WmSysRole role : roles) {
            roleNames.add(role.getRoleName());
        }
        return JSON.toJSONString(new ResponseBean(200,"SUCCESS",roleNames));
   // return userService.userInfo(uid);
    }

//    /*修改角色菜单权限*/
//    @PostMapping("/updateRoleMenu")
//    public String updateRoleMenu(String roleName,String menuName[],String nMenuName[]){
//        UserDto userDto=new UserDto();
//        WmSysRole wmSysRole=new WmSysRole();
//        wmSysRole.setRoleName(roleName);
//        userDto.setWmSysRole(wmSysRole);//赋值给dto的角色对象
//
//        List<WmSysMenu> wmSysMenuList=new ArrayList<WmSysMenu>();
//        for(String menuName1:menuName){
//            WmSysMenu wmSysMenu=new WmSysMenu();
//            wmSysMenu.setMenuName(menuName1);
//            wmSysMenuList.add(wmSysMenu);
//        }
//        userDto.setWmSysMenuList(wmSysMenuList);
//
//        List<WmSysMenu> nWmSysMenuList=new ArrayList<WmSysMenu>();
//        for(String nMenuName1:nMenuName){
//            WmSysMenu nWmSysMenu=new WmSysMenu();
//            nWmSysMenu.setMenuName(nMenuName1);
//            nWmSysMenuList.add(nWmSysMenu);
//        }
//        userDto.setNWmSysMenuList(nWmSysMenuList);
//        return userService.updateRoleMenu(userDto);
//    }

}
